package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: libin
 * @date: 2019/11/28 21:05
 * @version: 1.0
 * @describe:
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    /**
     * 预约下单
     * @param orderInfoVo
     * @return
     */
    public Result submit(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        //1.查询日期是否有预约设置(返回预约对象，要用到预约人数，已预约人数)
        OrderSetting orderSetting = orderSettingDao.findByDate(orderDate);
        if (null == orderSetting) {
            return new Result(false,"没有档期,请预约其他日期");
        }
        //2.预约是否已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number <= reservations) {
            return  new Result(false,"预约人数已满,请预约其他日期");
        }
        //3.根据手机号码查询是否是我们的会员
        Member member = memberDao.findByTelephone(telephone);
        //4.如果不是就自动注册会员
        if (null == member) {
            member=new Member();//空对象要初始化
            member.setPhoneNumber(telephone);
            member.setIdCard(orderInfoVo.getIdCard());
            member.setName(orderInfoVo.getName());
            member.setSex(orderInfoVo.getSex().toString());
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        Integer id = member.getId();//?
        //5.根据会员id，套餐id和预约日期查询是否重复预约
        Order order = new Order();
        order.setMemberId(id);
        order.setOrderDate(orderDate);
        order.setSetmealId(orderInfoVo.getSetmealId());
        List<Order> byCondition = orderDao.findByCondition(order);
        //6.如果没有就添加预约，下单
        if (CollectionUtil.isNotEmpty(byCondition)) {
            return new Result(false, "请不要重复预约");
        }
        Order orderNew = new Order();
        orderNew.setMemberId(id);
        orderNew.setOrderDate(orderDate);
        orderNew.setSetmealId(orderInfoVo.getSetmealId());
        orderNew.setOrderType(orderInfoVo.getOrderType());
        orderNew.setOrderStatus(Order.ORDERSTATUS_NO);
        orderDao.add(orderNew);
        //7.修改预约人数
        int i= orderSetting.getReservations() + 1;
        orderSetting.setReservations(i);
        //给预约人数表加一个版本version，防止预约人数超出
       int i1 = orderSettingDao.updateReservations(orderSetting);
       if (i1==0){
           //没有更新预约人数
           throw new RuntimeException("更新失败");
       }
        //8.返回预约成功,页面需要订单id，需要把orderNew返回
        return new Result(true, MessageConstant.ORDER_SUCCESS,orderNew);
    }

    /**
     * 查找订单详情
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
       return orderDao.findById4Detail(id);
    }
}
